package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.BatchRepairDeteMapper;
import com.indusfo.spc.mapper.BatchRepairFlowMapper;
import com.indusfo.spc.mapper.BatchRepairMapper;
import com.indusfo.spc.pojo.*;
import com.indusfo.spc.service.BatchRepairService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author liub
 * @Date 2019/12/6 8:51
 * @Version 1.0
 */

@Service
public class BatchRepairServiceImpl implements BatchRepairService {

    /**
     * 声明日志的静态变量
     */
    private static final Logger logger = LoggerFactory.getLogger(BatchRepairServiceImpl.class);

    @Resource
    private BatchRepairMapper batchRepairMapper;

    @Resource
    private BatchRepairFlowMapper batchRepairFlowMapper;

    @Resource
    private BatchRepairDeteMapper batchRepairDeteMapper;

    /**
     * 查询
     */
    @Override
    public JSONObject queryBatchRepair(BatchRepair batchRepair) {
        try {
            //获取页面的数据个数
            Integer pagesize = batchRepair.getPagesize();
            //获取查询的页数
            Integer pageindex = batchRepair.getPageindex();
            if (pagesize != null && pageindex != null) {
                batchRepair.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<BatchRepair> batchRepairList = batchRepairMapper.queryBatchRepair(batchRepair);

            //如果页数和页面数量都不为空的情况下进行计数
            if (batchRepairList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, batchRepairList, 0);
            } else {
                Integer count = batchRepairMapper.countBatchRepair(batchRepair);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, batchRepairList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    /**
     * 新增
     */
    @Override
    public JSONObject insertBatchRepair(BatchRepair batchRepair) {
        try {
            if (batchRepair.getBatchRepairId() != null) {
                throw new ModifyFailedException(HintMessage.INSERT_ID);
            }
            //初始化基础数据
            if (batchRepair.getDataState() == null) {
                batchRepair.setDataState(1);
            }
            //判重
            checkParam(batchRepair);
            //执行新增操作
            Integer row = batchRepairMapper.insertBatchRepair(batchRepair);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
            }
            return JSONObject.oK(HintMessage.INSERT_SUCCESS);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }

    }

    /**
     * 修改
     */
    @Override
    public JSONObject updateBatchRepair(BatchRepair batchRepair) {
        try {
            if (batchRepair.getBatchRepairId() == null) {
                throw new ParamsErrorException(HintMessage.UPDATE_ID);
            }
            //初始化基础数据
            if (batchRepair.getDataState() == null) {
                batchRepair.setDataState(1);
            }
            //判重
            checkParam(batchRepair);
            //执行修改操作
            int row = batchRepairMapper.updateBatchRepair(batchRepair);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
            }
            return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
        } catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 删除
     */
    @Override
    public JSONObject deleteBatchRepair(List<Integer> batchRepairIds, Integer dataState) {
        try {
            //空值判断
            if (dataState == null) {
                throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
            }
            if (batchRepairIds == null) {
                throw new ParamsErrorException(HintMessage.LIST_NULL);
            }
            //记录异常信息
            String msg = "";
            switch (dataState) {
                case 1:
                    msg = HintMessage.START_VALUE;
                    break;
                case 2:
                    msg = HintMessage.DELETE_VALUE;
                    break;
                case 3:
                    msg = HintMessage.STOP_VALUE;
                    break;
                default:
                    throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
            }
            //执行删除操作
            int row = batchRepairMapper.deleteBatchRepair(batchRepairIds, dataState);
            //声明msg字符串，用来存抛出的异常
            if (row == 0) {
                msg = msg + HintMessage.DEFEATED_VALUE;
            } else {
                msg = msg + HintMessage.SUCCESS_VALUE;
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 级联删除维修流程关联的工序、维修采集参数
     */
    @Transactional
    @Override
    public JSONObject deleteCascadeBatchRepair(List<Integer> batchRepairIds, Integer dataState) {
        //空值判断
        if (dataState == null) {
            throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
        }
        if (batchRepairIds == null) {
            throw new ParamsErrorException(HintMessage.LIST_NULL);
        }
        //记录异常信息
        String msg = "";
        switch (dataState) {
            case 1:
                msg = HintMessage.START_VALUE;
                break;
            case 2:
                msg = HintMessage.DELETE_VALUE;
                break;
            case 3:
                msg = HintMessage.STOP_VALUE;
                break;
            default:
                throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
        }
        //执行删除操作
        int row = batchRepairMapper.deleteBatchRepair(batchRepairIds, dataState);

        //根据传入的batchRepairIds查询对应的工序和维修采集参数
        for (Integer batchRepairId : batchRepairIds) {

            //级联删除维修下工序
            List<BatchRepairFlow> batchRepairFlowList = batchRepairFlowMapper.queryBatchRepairFlowByBatchRepairId(batchRepairId);
            if (batchRepairFlowList != null && !batchRepairFlowList.isEmpty()) {
                for (BatchRepairFlow batchRepairFlow : batchRepairFlowList) {
                    batchRepairFlowMapper.deleteBatchRepairFlowByBatchRepairFlowId(batchRepairFlow.getBatchRepairFlowId(), dataState);
                }
            }

            //级联删除维修采集参数
            List<BatchRepairDete> batchRepairDeteList = batchRepairDeteMapper.queryBatchRepairDeteByBatchRepairId(batchRepairId);
            if (batchRepairDeteList != null && !batchRepairDeteList.isEmpty()) {
                for (BatchRepairDete batchRepairDete : batchRepairDeteList) {
                    batchRepairDeteMapper.deleteBatchRepairDeteByBatchRepairDeteId(batchRepairDete.getBatchRepairDeteId(), dataState);
                }
            }
        }

        //声明msg字符串，用来存抛出的异常
        if (row == 0) {
            msg = msg + HintMessage.DEFEATED_VALUE;
        } else {
            msg = msg + HintMessage.SUCCESS_VALUE;
        }
        return JSONObject.oK(msg);
    }

    /**
     * 判重
     */
    public void checkParam(BatchRepair batchRepair) {
        //批次ID
        Integer batchId = batchRepair.getBatchId();
        //维修ID
        Integer repairId = batchRepair.getRepairId();
        //批次ID
        if (batchId == null) {
            throw new ParamsErrorException(HintMessage.BATCHID_NULL);
        }
        //维修ID
        if (repairId == null) {
            throw new ParamsErrorException(HintMessage.REPAIRID_NULL);
        }

        // 执行判重操作
        Integer Counts = batchRepairMapper.checkBatchRepair(batchRepair);
        if (Counts != 0) {
            throw new ParamsErrorException(HintMessage.REPEATING_DATA);
        }
    }

    /**
     * 批次流程维修工序
     */
    @Override
    public JSONObject saveBatchRepairFlow(BatchRepair batchRepair) {
        try {
            //判空
            if (batchRepair == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (batchRepair.getBatchRepairId() == null) {
                throw new ParamsErrorException(HintMessage.ORDERFLOWID_NULL);
            }
            //获取流程id
            Integer batchRepairId = batchRepair.getBatchRepairId();
            //获取list集合
            List<BatchRepairFlow> batchRepairFlowList = new ArrayList<>();
            batchRepairFlowList = batchRepair.getBatchRepairFlowList();
            for (BatchRepairFlow batchRepairFlow : batchRepairFlowList) {
                batchRepairFlow.setBatchRepairId(batchRepairId);
            }


            //获取数据库数据
            BatchRepairFlow batchRepairFlow1 = new BatchRepairFlow();
            batchRepairFlow1.setBatchRepairId(batchRepairId);
            List<BatchRepairFlow> oldbatchRepairFlowList = batchRepairFlowMapper.queryBatchRepairFlow(batchRepairFlow1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldbatchRepairFlowList != null) {
                for (BatchRepairFlow batchRepairFlow : oldbatchRepairFlowList
                ) {
                    oldIds.add(batchRepairFlow.getBatchRepairFlowId());
                }
            }
            if (batchRepairFlowList != null) {
                for (BatchRepairFlow batchRepairFlow : batchRepairFlowList
                ) {
                    newIds.add(batchRepairFlow.getBatchRepairFlowId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = batchRepairFlowMapper.deleteBatchRepairFlow(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (batchRepairFlowList != null) {
                for (BatchRepairFlow batchRepairFlow : batchRepairFlowList
                ) {
                    if (batchRepairFlow.getDataState() == null) {
                        batchRepairFlow.setDataState(1);
                    }


                    if (batchRepairFlow.getBatchRepairFlowId() != null) {
                        if (batchRepairFlow.getBatchRepairFlowId().equals(0)) {
                            batchRepairFlow.setBatchRepairFlowId(null);
                            // 执行判重操作
                            Integer Counts = batchRepairFlowMapper.checkBatchRepairFlow(batchRepairFlow);
                            if (Counts == 0) {
                                insertrow = insertrow + batchRepairFlowMapper.insertBatchRepairFlow(batchRepairFlow);
                            }

                        } else {
                            // 执行判重操作
                            Integer Counts = batchRepairFlowMapper.checkBatchRepairFlow(batchRepairFlow);
                            if (Counts == 0) {
                                updaterow = updaterow + batchRepairFlowMapper.updateBatchRepairFlow(batchRepairFlow);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 批次流程维修采集参数
     */
    @Override
    public JSONObject saveBatchRepairDete(BatchRepair batchRepair) {
        try {
            //判空
            if (batchRepair == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (batchRepair.getBatchRepairId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHREPAIRID_NULL);
            }
            if (batchRepair.getRepairDetailId() == null) {
                throw new ParamsErrorException(HintMessage.REPAIRDETAILID_NULL);
            }


            //获取流程id
            Integer batchRepairId = batchRepair.getBatchRepairId();
            Integer repairDetailId = batchRepair.getRepairDetailId();
            //获取list集合
            List<BatchRepairDete> batchRepairDeteList = new ArrayList<>();
            batchRepairDeteList = batchRepair.getBatchRepairDeteList();
            for (BatchRepairDete batchRepairDete : batchRepairDeteList) {
                batchRepairDete.setBatchRepairId(batchRepairId);
                batchRepairDete.setRepairDetailId(repairDetailId);
            }


            //获取数据库数据
            BatchRepairDete batchRepairDete1 = new BatchRepairDete();
            batchRepairDete1.setBatchRepairId(batchRepairId);
            batchRepairDete1.setRepairDetailId(repairDetailId);
            List<BatchRepairDete> oldbatchRepairDeteList = batchRepairDeteMapper.queryBatchRepairDete(batchRepairDete1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldbatchRepairDeteList != null) {
                for (BatchRepairDete batchRepairDete : oldbatchRepairDeteList
                ) {
                    oldIds.add(batchRepairDete.getBatchRepairDeteId());
                }
            }
            if (batchRepairDeteList != null) {
                for (BatchRepairDete batchRepairDete : batchRepairDeteList
                ) {
                    newIds.add(batchRepairDete.getBatchRepairDeteId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = batchRepairDeteMapper.deleteBatchRepairDete(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (batchRepairDeteList != null) {
                for (BatchRepairDete batchRepairDete : batchRepairDeteList
                ) {
                    if (batchRepairDete.getDataState() == null) {
                        batchRepairDete.setDataState(1);
                    }


                    if (batchRepairDete.getBatchRepairDeteId() != null) {
                        if (batchRepairDete.getBatchRepairDeteId().equals(0)) {
                            batchRepairDete.setBatchRepairDeteId(null);
                            // 执行判重操作
                            Integer Counts = batchRepairDeteMapper.checkBatchRepairDete(batchRepairDete);
                            if (Counts == 0) {
                                insertrow = insertrow + batchRepairDeteMapper.insertBatchRepairDete(batchRepairDete);
                            }

                        } else {
                            // 执行判重操作
                            Integer Counts = batchRepairDeteMapper.checkBatchRepairDete(batchRepairDete);
                            if (Counts == 0) {
                                updaterow = updaterow + batchRepairDeteMapper.updateBatchRepairDete(batchRepairDete);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }
}
